package se.statifyme.server;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import se.statifyme.server.store.EconomyStore;
import se.statifyme.server.store.EconomyStoreImpl;
import se.statifyme.server.store.KeyValueStore;
import se.statifyme.server.store.KeyValueStoreImpl;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

public class StatifyMeServerConfig extends GuiceServletContextListener {
	
	public static final String SERVICES_PACKAGE = "se.statifyme.server.services";
	
	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new ServletModule() {
			@Override
			protected void configureServlets() {
				bind(KeyValueStore.class).to(KeyValueStoreImpl.class);
				bind(EconomyStore.class).to(EconomyStoreImpl.class);
				
				EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("transactions-optional");
				bind(EntityManagerFactory.class).toInstance(entityManagerFactory);
				
				Map<String, String> params = new HashMap<String, String>();
                params.put("com.sun.jersey.config.property.packages", SERVICES_PACKAGE); 
				serve("/*").with(GuiceContainer.class, params);
			}
		});
	}

}
